package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private MovieService movieService;
    @RequestMapping("/index")
    public String index(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,//从链接取得变量值
                        Model model) {
        int pageSize =4;
        Map<String, Object> map = movieService.queryP(pageNo, pageSize);
        int totalRecords = (Integer) map.get("count");
        System.out.println(totalRecords);
        List<Movie> movielist=(List<Movie>) map.get("records");
        //计算总页数
        Integer pageCount = (totalRecords % pageSize == 0) ? (totalRecords / pageSize) : ((totalRecords / pageSize) + 1);
        model.addAttribute("currentPage",pageNo);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("movielist",movielist);
        return "index";
    }
}
